package cn.it.ygh.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.it.ygh.pojo.House;
import cn.it.ygh.pojo.User;
import cn.it.ygh.service.HouseService;
import cn.it.ygh.utils.ConfigReader;
import cn.it.ygh.utils.FileUtil;
import cn.it.ygh.utils.Md5Util;

@Controller
@RequestMapping("/house")
public class HouseController {
	
	@Autowired
	private HouseService houseService;
	
   /**
    * 锟斤拷取锟斤拷锟斤拷锟矫伙拷锟侥憋拷
    * @param
    * @return 
    */
	/*@RequestMapping(value = "/houseList.do")
	public ModelAndView userList(){
		List<House> houseList = houseService.searchHouse();
		ModelAndView mv = new ModelAndView();
		mv.addObject("houseList", houseList);
		mv.setViewName("../house/jsp/houseList");
		
		return mv;
	}*/
	/**
	 * 锟斤拷转锟斤拷锟斤拷锟斤拷楼锟斤拷锟斤拷息锟斤拷锟斤拷
	 * @param
	 * @return 
	 */
	@RequestMapping(value = "/addLoad.do")
	public ModelAndView addLoad(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("../../house/jsp/housePublish");
		return mv;
	}	
    /**
    * 锟斤拷锟铰ワ拷锟斤拷锟较拷锟斤拷囟锟斤拷锟�
    * @param
    * @return 
    */
	@RequestMapping(value = "/add.do",method = RequestMethod.POST)
	public String add(House house,HttpSession session){
        
		User u = (User) session.getAttribute("u");
		house.setHouseUserAccount(u.getUserAccount());
		house.setHouseIsAccept("否");

		houseService.addHouse(house);
		return "redirect:/users/jsp/index.jsp";
	}
			
	/**
     * 锟睫革拷锟斤拷息锟斤拷取锟斤拷锟斤拷转锟斤拷锟睫革拷锟斤拷息锟斤拷图
     * @param id
     * @return
     */
	@RequestMapping(value = "/editLoad.do")
	public ModelAndView editLoad(@RequestParam(value = "id") int id){
		House house= houseService.findById(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("house", house);
		mv.setViewName("../../house/jsp/editHouse");
		return mv;
	}
	/**
     * 锟睫革拷锟斤拷息
     * @param user
     * @return
     */
	@RequestMapping(value = "/edit.do",method = RequestMethod.POST)
	public String edit(House house){
		houseService.updateHouse(house);
		return "redirect:/house/jsp/houseList.jsp";
	}
	/**
     * 删锟斤拷锟矫伙拷
     * @param id
     * @return
     */
	@RequestMapping(value = "/delete.do")
	public String  delete(@RequestParam(value = "id") int id){
		houseService.deleteHouse(id);
		return "redirect:/house/jsp/houseList.jsp";
	}
	
	@RequestMapping(value = "/upload.do")
	@ResponseBody
	public  Map<Object,Object> upload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, ModelMap model) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			// 文件是否为空
			if (file.getSize() == 0 || file.isEmpty()) {
				System.out.println("文件为空");
			}
			// 文件是否过大
			if (file.getSize() > ConfigReader.getUpload_img_maxsize()) {
				System.out.println("文件过大");
			}

			String path = request.getSession().getServletContext().getRealPath("/image"); 
			String path1 = request.getContextPath()+"/image/";
			String ori_fileName = file.getOriginalFilename();
			System.out.println(ori_fileName);
			String md5_fileName = Md5Util.getFileMD5String(file.getInputStream()) + "."
					+ FileUtil.getExtensionName(ori_fileName);
			System.out.println("path:" + path);
			System.out.println("ori_fileName:" + ori_fileName);
			System.out.println("md5_filename:" + md5_fileName);
			File targetFile = new File(path, md5_fileName);
			if (!targetFile.exists()) {
				file.transferTo(targetFile);
				// 是否是图片
				if (!isImage(targetFile)) {
					targetFile.delete();
				}
			}
			map.put("path", path1+md5_fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("---------------- upload img end ---------------------");
		return map;
	}

	/**
	 * 判断文件是否是图片
	 * @param file
	 * @return
	 * @throws IOException 
	 */
	private static boolean isImage(File file) throws IOException {
		BufferedImage bi = ImageIO.read(file);
		if(bi == null){
		   return false;
		}
		return true;
	}
	
	/*下拉框数据库引入
	 * 
	 * */
	@RequestMapping("/getAllAgent.do")
	public ModelAndView getAllAgent(){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("agentList",houseService.findAll());
		modelAndView.setViewName("../house/jsp/housePublish");
		return modelAndView;
	}
	
   /*获取房源信息
    * */
	/*@RequestMapping("/getAllHouse.do")
	public ModelAndView getAllHouse(){
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("houseList",houseService.searchHouse());
		modelAndView.setViewName("../house/jsp/houseList");
		return modelAndView;
	}*/
	
	/*房源分页处理*/
	@RequestMapping("/getPageData.do")
	public Map<Object,Object> getPageData(@RequestParam(value = "pageStart") int pageStart,@RequestParam(value="pageSize") int pageSize){
		List<House> houseList = houseService.getPageData(pageStart, pageSize);
		int total = houseService.getTotal();
		Map<Object,Object> info = new HashMap<Object, Object>();
		info.put("data",houseList);
		info.put("total",total);
		return info;
	}
}
